Ext.define('console.testPaperWindow', {
    extend: 'Ext.ux.desktop.Module',

    requires: [
        'console.store.testPaperStore',
        'Ext.data.ArrayStore',
        'Ext.util.Format',
        'Ext.grid.Panel',
        'Ext.grid.RowNumberer'
    ],

    id: 'testPaperWindow',
    init: function () {
        this.launcher = {
            text: '问卷管理',
            iconCls: 'icon-grid'
        };
    },

    createWindow: function () {
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('testPaperWindow');
        if (!win) {
            win = desktop.createWindow({
                id: 'testPaperWindow',
                title: '问卷管理',
                width: 1200,
                height: 620,
                iconCls: 'icon-grid',
                animCollapse: false,
                constrainHeader: true,
                layout: "border",
                items: [
                    {
                        title: '问卷列表',
                        border: false,
                        xtype: 'grid',
                        region: 'west',
                        width: '40%',
                        split: true,
                        itemId: 'testPaper',
                        store: Ext.createWidget('testPaperStore'),
                        columns: [
                            {
                                text: "代码",
                                sortable: true,
                                width: '10%',
                                dataIndex: 'paperId'
                            }, {
                                text: "问卷名称",
                                sortable: true,
                                width: '30%',
                                dataIndex: 'name'
                            },
                            {
                                text: "问卷类型",
                                sortable: true,
                                dataIndex: 'type',
                                width: '30%',
                                renderer: function (val) {
                                    if (val == 'A') {
                                        return "内部互评问卷";
                                    } else {
                                        return "患者评价问卷";
                                    }
                                }
                            }, {
                                text: "题目数量",
                                sortable: true,
                                flex: 1,
                                dataIndex: 'questionCount'
                            }, {
                                text: "备注",
                                sortable: true,
                                flex: 1,
                                dataIndex: 'mark'
                            }
                        ],
                        listeners: {
                            select: function (t, r, i, o) {
                                var questions = this.ownerCt.child("#details");
                                questions.getStore().load({params: {paperId: r.data.paperId}});
                            }
                        },
                        tbar: [{
                            text: '添加问卷',
                            tooltip: '添加一组数据',
                            iconCls: 'add',
                            handler: this.addPaper
                        }, '-', {
                            text: '修改问卷',
                            tooltip: '修改一组数据',
                            iconCls: 'option',
                            handler: this.optionPaper
                        }, '-', {
                            text: '删除问卷',
                            tooltip: '删除一组数据',
                            iconCls: 'remove',
                            handler: this.removePaper
                        }, '-', {
                            text: '刷新',
                            tooltip: '刷新数据',
                            iconCls: 'refresh',
                            handler: this.refresh
                        }]
                    },
                    {
                        title: '问卷问题列表',
                        border: false,
                        xtype: 'grid',
                        region: 'center',
                        itemId: 'details',
                        split: true,
                        store: Ext.createWidget('questionStore', {autoLoad: false}),
                        columns: [
                            {
                                text: "题号",
                                width: '10%',
                                sortable: true,
                                dataIndex: 'id'
                            },
                            {
                                text: "内容",
                                width: '80%',
                                sortable: true,
                                dataIndex: 'content'
                            },
                            {
                                text: "备注",
                                flex: 1,
                                sortable: true,
                                dataIndex: 'mark'
                            }
                        ],
                        tbar: [{
                            text: '添加问题',
                            tooltip: '添加一组数据',
                            iconCls: 'add',
                            handler: this.addDetail
                        } , '-', {
                            text: '删除问题',
                            tooltip: '删除一组数据',
                            iconCls: 'remove',
                            handler: this.removeDetail
                        }, '-', {
                            text: '刷新',
                            tooltip: '刷新数据',
                            iconCls: 'refresh',
                            handler: this.refresh
                        }]

                    }
                ]
            });
        }
        return win;
    },


    addPaper: function () {
        Ext.create('Ext.window.Window', {
            modal: true,
            items: [{
                xtype: 'form',
                defaults: {
                    labelAlign: 'left',
                    margin: '10 10 10 10',
                    width: '90%',
                    xtype: 'textfield'
                },
                items: [{
                    xtype: 'combobox',
                    fieldLabel: '类型',
                    allowBlank: false,
                    name: 'type',
                    editable: false,
                    store: Ext.create('Ext.data.Store', {
                        fields: ['itemCode', 'itemName'],
                        data: [


                            {itemCode: 'A', itemName: '内部互评'},
                            {itemCode: 'B', itemName: '患者评科室'},
                            {itemCode: 'C', itemName: '领导评价行政'},
                            {itemCode: 'D', itemName: '手术科室评价麻醉'}


                        ]
                    }),
                    queryMode: 'local',
                    displayField: 'itemName',
                    valueField: 'itemCode'
                }, {
                    name: 'name',
                    fieldLabel: '问卷名称',
                    allowBlank: false
                }, {
                    name: 'mark',
                    fieldLabel: '备注'
                }],
                buttons: [{
                    text: '重置',
                    handler: function () {
                        this.up('form').getForm().reset();
                    }
                }, {
                    text: '提交',
                    handler: function () {
                        var form = this.up('form').getForm();
                        if (form.isValid()) {
                            form.submit({
                                url: 'testPaper/addPaper',
                                success: function (form, action) {
                                    Ext.Msg.alert('', '添加成功', function () {
                                        form.owner.ownerCt.close();
                                        Ext.getCmp('testPaperWindow').query('grid')[0].store.reload();
                                    }, this);
                                },
                                failure: function (form, action) {
                                    Ext.Msg.alert('', '添加信息失败');
                                }
                            });
                        }
                    }
                }]
            }]
        }).show();
    },
    optionPaper: function () {
        var row = this.ownerCt.ownerCt.getSelectionModel().selected.items;
        if (row.length == 0) {
            Ext.Msg.alert('', '需要选中一行记录');
            return;
        }
        var data = row[0].data;
        var win = Ext.create('Ext.window.Window', {
            modal: true,
            items: [{
                xtype: 'form',
                defaults: {
                    labelAlign: 'left',
                    margin: '10 10 10 10',
                    width: '90%',
                    xtype: 'textfield'
                },
                items: [, {
                    name: 'paperId',
                    fieldLabel: '问卷号',
                    allowBlank: false,
                    readOnly: true
                }, {
                    xtype: 'combobox',
                    fieldLabel: '类型',
                    allowBlank: false,
                    name: 'type',
                    editable: false,
                    store: Ext.create('Ext.data.Store', {
                        fields: ['itemCode', 'itemName'],
                        data: [
                            {itemCode: 'A', itemName: '内部互评'},
                            {itemCode: 'B', itemName: '患者评科室'},
                            {itemCode: 'C', itemName: '领导评价行政'},
                            {itemCode: 'D', itemName: '手术科室评价麻醉'}
                        ]
                    }),
                    queryMode: 'local',
                    displayField: 'itemName',
                    valueField: 'itemCode'
                }, {
                    name: 'name',
                    fieldLabel: '问卷名称',
                    allowBlank: false
                }, {
                    name: 'mark',
                    fieldLabel: '备注'
                }],
                buttons: [{
                    text: '重置',
                    handler: function () {
                        this.up('form').getForm().reset();
                    }
                }, {
                    text: '提交',
                    handler: function () {
                        var form = this.up('form').getForm();
                        if (form.isValid()) {
                            form.submit({
                                url: 'testPaper/updatePaper',
                                success: function (form, action) {
                                    Ext.Msg.alert('', '操作成功', function () {
                                        form.owner.ownerCt.close();
                                        Ext.getCmp('testPaperWindow').query('grid')[0].store.reload();
                                    }, this);
                                },
                                failure: function (form, action) {
                                    Ext.Msg.alert('', '操作信息失败');
                                }
                            });
                        }
                    }
                }]
            }]
        });
        win.query('form')[0].getForm().setValues(data);
        win.show();
    },
    removePaper: function () {
        var row = this.ownerCt.ownerCt.getSelectionModel().selected.items;
        if (row.length == 0) {
            Ext.Msg.alert('', '需要选中一行记录');
            return;
        }
        var paperId = row[0].data.paperId;
        Ext.MessageBox.confirm('', '您确定要删除吗?', function (opt) {
            if (opt == 'yes') {
                Ext.Ajax.request({
                    url: 'testPaper/deletePaper',
                    params: {paperId: paperId},
                    success: function (response) {
                        var res = Ext.decode(response.responseText);
                        if (res.success) {
                            Ext.Msg.alert('删除成功', '删除成功', function () {
                                Ext.getCmp('testPaperWindow').query('grid')[0].store.reload();
                            }, this);
                        } else {
                            Ext.Msg.alert('操作失败', res.failure);
                        }
                    },
                    failure: function (action) {
                        Ext.Msg.alert('', '操作信息失败');
                    }
                });
            }
        })
    },
    addDetail: function () {
        var row = this.ownerCt.ownerCt.ownerCt.child("#testPaper").getSelectionModel().selected.items;
        if (row.length == 0) {
            Ext.Msg.alert('', '需要选中一行分组');
            return;
        }
        Ext.create('Ext.window.Window', {
            modal: true,
            title: '问题列表',
            items: [{
                border: false,
                xtype: 'grid',
                width: 1024,
                height: 600,
                multiSelect: true,
                itemId: 'selectQuestions',
                selModel: Ext.create('Ext.selection.CheckboxModel'),
                store: Ext.createWidget('questionStore', {
                    listeners: {
                        beforeload: function (store, options) {
                            var extraParams = {paperId: row[0].data.paperId, filter: 'filter'};
                            Ext.apply(store.proxy.extraParams, extraParams);
                        }
                    }
                }),
                columns: [
                    {
                        text: "题号",
                        width: '10%',
                        sortable: true,
                        dataIndex: 'id'
                    },
                    {
                        text: "内容",
                        flex:1,
                        sortable: true,
                        dataIndex: 'content'
                    },
                    {
                        text: "备注",
                        flex: 1,
                        hidden:true,
                        sortable: true,
                        dataIndex: 'mark'
                    }
                ],
                tbar: [{
                    text: '确认添加',
                    tooltip: '确认添加',
                    iconCls: 'add',
                    handler: function () {
                        var subGrid = this.ownerCt.ownerCt;
                        var subRow = subGrid.getSelectionModel().selected.items;
                        if (subRow.length == 0) {
                            Ext.Msg.alert('', '需要选中一行记录');
                            return;
                        } else {
                            var ids = new Array();
                            for (var i = 0; i < subRow.length; i++) {
                                ids.push(subRow[i].data.id);
                            }
                            Ext.Ajax.request({
                                url: 'testPaper/addDetail',
                                params: {questionId: ids, paperId: row[0].data.paperId},
                                success: function (response) {
                                    var res = Ext.decode(response.responseText);
                                    if (res.success) {
                                        Ext.Msg.alert('添加成功', '添加成功', function () {
                                            subGrid.getStore().reload();
                                            Ext.getCmp('testPaperWindow').query('grid')[1].getStore().load({params: {paperId: row[0].data.paperId}});
                                        }, this);
                                    } else {
                                        Ext.Msg.alert('操作失败', res.failure);
                                    }
                                },
                                failure: function (action) {
                                    Ext.Msg.alert('', '操作信息失败');
                                }
                            });
                        }
                    }
                }]
            }]
        }).show();
    },


    removeDetail: function () {
        var row = this.ownerCt.ownerCt.ownerCt.child("#testPaper").getSelectionModel().selected.items;
        if (row.length == 0) {
            Ext.Msg.alert('', '需要选中一行分组');
            return;
        }
        var subRow = this.ownerCt.ownerCt.getSelectionModel().selected.items;
        if (subRow.length == 0) {
            Ext.Msg.alert('', '需要选中一行问题');
            return;
        }
        var id = subRow[0].data.id;
        var paperId = row[0].data.paperId;
        Ext.MessageBox.confirm('', '您确定要删除吗?', function (opt) {
            if (opt == 'yes') {
                Ext.Ajax.request({
                    url: 'testPaper/deleteDetail',
                    params: {questionId: id, paperId: paperId},
                    success: function (response) {
                        var res = Ext.decode(response.responseText);
                        if (res.success) {
                            Ext.Msg.alert('删除成功', '删除成功', function () {
                                Ext.getCmp('testPaperWindow').query('grid')[1].getStore().load({params: {paperId: row[0].data.paperId}});
                            }, this);
                        } else {
                            Ext.Msg.alert('操作失败', res.failure);
                        }
                    },
                    failure: function (action) {
                        Ext.Msg.alert('', '操作信息失败');
                    }
                });
            }
        })
    },
    refresh: function () {
        this.ownerCt.ownerCt.getStore().reload();
    }
});

